﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading;

namespace AtDevice
{
    class AtLogger
    {
        private static TextWriter _stream = Console.Out;
        public static bool DoLogging = false;

        internal static void LogReceived(string line)
        {
            lock(_stream) {
                if(DoLogging)
                    _stream.WriteLine(String.Format("{0} <- [{1}]", Thread.CurrentThread.ManagedThreadId, ToReadable(line)));
            }
        }

        internal static void LogSent(byte[] array)
        {
            if(DoLogging)
                _stream.WriteLine(String.Format("{0} -> [{1}]", Thread.CurrentThread.ManagedThreadId, ToReadable(new ASCIIEncoding().GetString(array))));
        }

        private static string ToReadable(string readable)
        {
            readable = readable.Replace("\n", "\\n");
            readable = readable.Replace("\r", "\\r");
            return readable;
        }

        internal static void LogInfo(string info)
        {
            lock(_stream)
            {
                if(DoLogging)
                    _stream.WriteLine(String.Format("{0} {1}", Thread.CurrentThread.ManagedThreadId, info));
            }
        }
    }
}
